package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;

@Controller
public class CartController {
    @Resource
    private CartFeignClient cartFeignClient;

    //加入购物车
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes) {
        cartFeignClient.addCart(skuId, skuNum);
        redirectAttributes.addAttribute("pid", skuId);
        redirectAttributes.addAttribute("sn", skuNum);
        return "redirect:http://cart.gmall.com/toCart.html";
    }

    //成功页面去购物车
    @GetMapping("/toCart.html")
    public String toCart(Long pid, Integer sn, Model model) {
        CartInfo cartInfo = cartFeignClient.toCart(pid);
        if (cartInfo != null){
            cartInfo.setSkuNum(sn);
            model.addAttribute(cartInfo);
            return "cart/addCart";
        }else {
            //之前未登录是有购物车  用户登录了 没有购物车 空指针
            return "redirect:http://www.gmall.com";
        }
    }

    //去购物车结算
    @GetMapping("/cart.html")
    public String cart(){
        return "cart/index";
    }
}
